<?php

namespace App\Listeners\Subscriber;

use App\Models\Inspection;
use App\Models\Inspection\Report;
use App\Models\Inspection\Circle;
use Illuminate\Events\Dispatcher;
use App\Exceptions\GeneralException;
use App\Events\Inspection\CreatedEvent;
use App\Repositories\ProjectRepository;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class InspectionEventSubscriber implements ShouldQueue
{
	/**
	 * Date: 01/05/2018
	 * @author George
	 * @param CreatedEvent $event
	 * @return Report|\Illuminate\Database\Eloquent\Model
	 * @throws GeneralException
	 */
	public function onCreated(CreatedEvent $event)
	{
		/**
		 * @var Inspection $inspection
		 */
		$inspection = $event->inspection;

		$report = Report::query()
			->where('date', date('Y-m-d'))
			->where('project_id', $inspection->project_id)->first();
		// 判断是否存在当天记录
		if ($report) {
			// 如果存在则遍历圈信息
			$circles = $report->circles;
			$report->node_inspected += 1;
			$report->node_rate = ($report->node_inspected / $report->nodes_count) * 100;

			foreach ($circles as &$circle) {
				// 判断巡检点的所属圈是否与当前圈ID一致
				if ($circle['id'] === $inspection->circle_id) {
					// 如果一致则更新圈属性中的点完成数量
					$circle['node_inspected'] += 1;
					// 根据圈的节点数量和已完成的点数量计算圈完成率
					$circle['rate'] = ($circle['node_inspected'] / $circle['nodes_count']) * 100;
					// 如果单个圈达到了100%，则更新圈完成数量，和完成率
					if ($circle['rate'] === 100) {
						$report->circles_inspected += 1;
						$report->circle_rate = ($report->circles_inspected / $report->circles_count) * 100;
					}
				}
			}
			$report->circles = $circles;
			$report->save();
		} else {
			$projectRepository = new ProjectRepository();
			// 获取巡检的项目相关设置
			$project = $projectRepository->getInspectionSetting($inspection->project_id);
			if ($project) {
				// 遍历圈数据，生成报表数据结构
				foreach ($project->circles as $circle) {
					// 如果与巡检记录的圈ID 一致则更新完成节点数量，和完成率
					if ($circle->id === $inspection->circle_id) {
						$circle->node_inspected = 1;
						/**
						 * @var Circle $circle
						 * 记录圈的巡检点数量
						 */
						$circle->nodes_count = $circle->nodes()->count();
						// 计算圈完成率
						$circle->rate = ($circle->node_inspected / $circle->nodes_count) * 100;
					} else {
						$circle->node_inspected = 0;
						/**
						 * @var Circle $circle
						 * 记录圈的巡检点数量
						 */
						$circle->nodes_count = $circle->nodes()->count();
						// 计算圈完成率
						$circle->rate = 0;
					}
				}
				// 创建报表数据结构
				$attributes = [
					'project_id' => $inspection->project_id,
					'project_name' => $project->name,
					'date' => date('Y-m-d'),
					// 线路总数
					'lines_count' => $project->lines_count,
					// 圈总数
					'circles_count' => $project->circles_count,
					// 点总数
					'nodes_count' => $project->nodes_count,
					// 圈完成率
					'circle_rate' => 0,
					// 已完成圈数量
					'circles_inspected' => 0,
					// 点完成率
					'node_rate' => (1 / $project->nodes_count) * 100,
					// 异常点
					'node_error' => 0,
					// 准点率
					'node_punctuality' => 0,
					// 已完成点数量
					'node_inspected' => 1,
					// 项目的圈信息
					'circles' => $project->circles->toArray(),
				];
				// 存入MongoDB 数据库
				return Report::create($attributes);
			}
			throw new GeneralException('未找到对应的项目信息');
		}
	}

	/**
	 * 为订阅者注册监听器
	 *
	 * Date: 23/03/2018
	 * @author George
	 * @param Dispatcher $events
	 */
	public function subscribe(Dispatcher $events)
	{
		$events->listen(
			'App\Events\Inspection\CreatedEvent',
			'App\Listeners\Subscriber\InspectionEventSubscriber@onCreated'
		);
	}
}
